package com.card.admin.servelt;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.card.admin.pojo.Admin;
import com.card.admin.pojo.AdminManager;
import com.card.admin.pojo.SellerLog;
import com.card.log.LogType;
import com.card.log.LogUtils;
import com.card.setting.GameSetting;
import com.card.user.User;
import com.card.user.UserCurrency.CurrencyKind;
import com.card.user.UserUtils;
import com.xiaoao.net.utils.PubFunc;
import com.xiaoao.net.utils.StringUtil;

public class DoActionServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		res.setContentType("text/plain;charset=utf-8");
		req.setCharacterEncoding("utf-8");
		String action = req.getParameter("action");
		if ("wx".equals(action)) {
			String gm = req.getParameter("goumai");
			String dl = req.getParameter("daili");

			String andoridxz = req.getParameter("andoridxz");
			String iosxz = req.getParameter("iosxz");
			String DayRewardGold = req.getParameter("DayRewardGold");
			String ExchangeScale = req.getParameter("ExchangeScale");
			if (!StringUtil.NullOrEmpty(gm)) {
				GameSetting.getInstance().setWxBuy(gm);
			}
			if (!StringUtil.NullOrEmpty(dl)) {
				GameSetting.getInstance().setWxDl(dl);
			}
			if (!StringUtil.NullOrEmpty(andoridxz)) {
				GameSetting.getInstance().setAndroidXiazai(andoridxz);
			}
			if (!StringUtil.NullOrEmpty(iosxz)) {
				GameSetting.getInstance().setIosXiazai(iosxz);
			}

			if (!StringUtil.NullOrEmpty(DayRewardGold)) {
				GameSetting.getInstance().setDayRewardGold(PubFunc.parseInt(DayRewardGold));
			}
			if (!StringUtil.NullOrEmpty(ExchangeScale)) {
				GameSetting.getInstance().setExchangeScale(PubFunc.parseInt(ExchangeScale));
			}
			GameSetting.getInstance().saveWx();
			req.getRequestDispatcher("/wx.jsp").forward(req, res);
			return;
		} else if ("resetpwd".equals(action)) {
			String userName = req.getParameter("username");
			String password = req.getParameter("password");
			String newpwd = req.getParameter("newpwd");
			String newpwd2 = req.getParameter("newpwd2");
			if (!StringUtil.equals(newpwd, newpwd2)) {
				req.getRequestDispatcher("/info.jsp?info=1").forward(req, res);
				return;
			}
			Admin admin = AdminManager.getInstance().getAdmin(userName);
			if (admin != null && StringUtil.equals(password, admin.getPassword()) && admin.isFree()) {
				admin.setPassword(newpwd);
				AdminManager.getInstance().changePwd(admin.getId() + "", newpwd);
				req.getRequestDispatcher("/index.jsp").forward(req, res);
				return;
			}
			req.getRequestDispatcher("/info.jsp?info=2").forward(req, res);
			return;
		} else if ("addagent".equals(action)) {
			String userName = req.getParameter("username");
			String password = req.getParameter("password");
			String phone = req.getParameter("phone");
			String roomsettings = req.getParameter("roomsetting");
			int fatherId = Integer.parseInt(req.getParameter("fatherId"));
			Admin admin = AdminManager.getInstance().getAdmin(userName);
			if (admin != null) {
				req.getRequestDispatcher("/info.jsp?info=3").forward(req, res);
				return;
			}
			AdminManager.getInstance().addAgent(fatherId, userName, password, phone);
			System.out.println("添加代理成功");
			req.getRequestDispatcher("/agent.jsp").forward(req, res);
			return;
		} else if ("czagent".equals(action)) {
			String id = req.getParameter("id");
			int addGold = PubFunc.parseInt(req.getParameter("gold"));
			if (addGold <= 0) {
				JSONObject js = new JSONObject("{'ok':1,'msg':'输入信息有误'}");
				res.getWriter().print(js);
				return;
			}
			AdminManager.getInstance().addGold(id, addGold, 0);
			req.getRequestDispatcher("/agent.jsp").forward(req, res);
			return;
		} else if ("sellerGold".equals(action)) {
			String id = req.getParameter("name");
			int addGold = PubFunc.parseInt(req.getParameter("gold"));
			User user = UserUtils.getUserByPrimaryKey(id);
			if (user == null) {
				JSONObject js = new JSONObject("{'ok':1,'msg':'用户找不到'}");
				res.getWriter().print(js);
				return;
			}
			if (addGold <= 0) {
				JSONObject js = new JSONObject("{'ok':1,'msg':'输入信息有误'}");
				res.getWriter().print(js);
				return;
			}
			Admin admin = (Admin) req.getSession().getAttribute("admin");
			int group = admin.getGroup();
			admin = AdminManager.getInstance().getAdminById(admin.getId() + "");
			if (admin == null || !admin.isFree()) {
				JSONObject js = new JSONObject("{'ok':1,'msg':'您的权限被删除,请联系我的工作人员'}");
				res.getWriter().print(js);
				return;
			}
			if (group == 1) {
				int oldGold = user.userCurrency().get(CurrencyKind.Diamonds);
				user.userCurrency().add(CurrencyKind.Diamonds, addGold);
				LogUtils.addSpendLog(user, LogType.GMFK.getDesc() + admin.getName(), addGold);
				int newGold = user.userCurrency().get(CurrencyKind.Diamonds);
				user.sendUserUpdateMessage();
			} else {
				if (admin.getGold() < addGold) {
					JSONObject js = new JSONObject("{'ok':1,'msg':'您的房卡不足'}");
					res.getWriter().print(js);
					return;
				}
				int oldGold = user.userCurrency().get(CurrencyKind.Diamonds);
				user.userCurrency().add(CurrencyKind.Diamonds, addGold);
				LogUtils.addSpendLog(user, LogType.GMFK.getDesc() + admin.getName(), addGold);
				int newGold = user.userCurrency().get(CurrencyKind.Diamonds);
				user.sendUserUpdateMessage();
				admin.setGold(admin.getGold() - addGold);
				admin.saveDB();
				admin.addSellerLog(new SellerLog(id, user.userExtra().getNickName(), oldGold, newGold));
			}
			JSONObject js = new JSONObject("{'ok':1,'msg':'成功'}");
			res.getWriter().print(js);
			return;
		}
		req.getRequestDispatcher("/agent.jsp").forward(req, res);
	}

}
